#!/bin/bash

############## 初始化mysql ##############1
# mysql配置文件
cat > /etc/my.cnf <<EOF_mysql
[mysqld]
datadir=/data/mysql
character-set-server=utf8
collation-server=utf8_general_ci
socket=/var/lib/mysql/mysql.sock
user=mysql
log-bin=mysql-bin
relay-log = relay-mysql
relay-log-index = relay-mysql.index
server-id=103
log-slave-updates
slave-skip-errors=all
skip-name-resolve
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
expire_logs_day=3
default_storage_engine=INNODB
symbolic-links=0
group_concat_max_len=200000
max_allowed_packet=20M
key_buffer_size=128M
innodb_buffer_pool_size=3072M
innodb_flush_method = O_DIRECT
sync_binlog=0
innodb_flush_log_at_trx_commit=0
thread_cache_size=16
innodb_write_io_threads=8
innodb_support_xa=0
max_connections=800
slow_query_log = 1
slow_query_log_file =/data/mysql/host-slow.log
long_query_time = 0.1
tmpdir=/data/tmp
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF_mysql

# 数据目录环境配置
mkdir -p /data/tmp
mkdir -p /data/mysql
chown mysql.mysql /data/tmp
chown mysql.mysql /data/mysql
mysql_install_db --user=mysql --datadir=/data/mysql
touch /var/log/mysqld.log
chown mysql.root /var/log/mysqld.log

# mysql启动初始化密码
/etc/init.d/mysql start
mysqladmin -uroot password 123456

# 创建radius表结构
cat > /tmp/radius_data_init.sql << EOF_radius
CREATE DATABASE Radius;
grant all privileges on Radius.* to 'radius'@'localhost' identified by '1234-qwer';
flush privileges;
use Radius;
drop table if exists RadiusParameters;
create table RadiusParameters(id int(11) unsigned NOT NULL auto_increment,ssid varchar(32) NOT NULL,parameterName varchar(64) NOT NULL,parameterValue int(11) NOT NULL,PRIMARY KEY (id),Key(parameterName(32)));
insert into RadiusParameters(ssid,parameterName,parameterValue) values ('i-Guangdong','maxOnlineDevices',3),('i-Guangdong','passwordExpiredTime',14),('i-Guangdong','deviceExpiredTime',14),('i-Guangdong','sessiontTimeout',7200),('i-Guangdong','idleTimeout',900);
drop table if exists nas;
CREATE TABLE nas(id int(10) NOT NULL auto_increment,ssid varchar(32) NOT NULL,nasname varchar(128) NOT NULL,shortname varchar(32),type varchar(30) DEFAULT 'other',ports int(5),secret varchar(60) DEFAULT 'secret' NOT NULL,server varchar(64),community varchar(50),description varchar(200) DEFAULT 'RADIUS Client',PRIMARY KEY(id),KEY nasname(nasname));
insert into nas(ssid,nasname,shortname,secret) values ('i-Guangdong',"121.8.183.252","zjgc","testing123");
EOF_radius

mysql -hlocalhost -uroot -p123456 < /tmp/radius_data_init.sql

############## 初始化mongodb1 port:30004 ##############
# GDRadius实例创建目录
mkdir -p /data/GDRadius/Data
mkdir -p /data/GDRadius/Log
mkdir -p /data/GDRadius/Config
mkdir -p /data/GDRadius/Keys

# 生成配置文件
cat > /data/GDRadius/Config/GDRadius.conf <<EOF_GDRadius
processManagement :
 fork : true
 pidFilePath :  /data/GDRadius/GDRadius.pid
systemLog:
 destination : file
 path : /data/GDRadius/Log/GDRadius.log
 logAppend : true

storage:
 dbPath : /data/GDRadius/Data
 engine : wiredTiger
 directoryPerDB : true

net:
 port : 30004
 maxIncomingConnections : 2000

auditLog:
 destination : file
 format : JSON
 path : /data/GDRadius/Log/iGDAduit.json
 filter : '{ atype : {\$nin : ["authenticate"]} }'
EOF_GDRadius

# 初始化实例30004设置密码
mongod -f /data/GDRadius/Config/GDRadius.conf
mongo --host 127.0.0.1 --port 30004 --quiet 'admin' <<EOJS
db.createUser(
  {
        user:"admin",
        pwd:"1234-qwer",
        roles:
		[ 	{ "role" : "readWrite", "db" : "admin" }, 
			{ "role" : "dbAdmin", "db" : "admin" }, 
			{ "role" : "userAdmin", "db" : "admin" }, 
			{ "role" : "clusterAdmin", "db" : "admin" }, 
			{ "role" : "readAnyDatabase", "db" : "admin" }, 
			{ "role" : "readWriteAnyDatabase", "db" : "admin" }, 
			{ "role" : "userAdminAnyDatabase", "db" : "admin" }, 
			{ "role" : "dbAdminAnyDatabase", "db" : "admin" } ]
    })
EOJS

mongod --shutdown --dbpath /data/GDRadius/Data/

# 启动密码认证
cat > /data/GDRadius/Config/GDRadius.conf <<EOF_GDRadius
processManagement :
 fork : true
 pidFilePath :  /data/GDRadius/GDRadius.pid
systemLog:
 destination : file
 path : /data/GDRadius/Log/GDRadius.log
 logAppend : true

storage:
 dbPath : /data/GDRadius/Data
 engine : wiredTiger
 directoryPerDB : true

net:
 port : 30004
 maxIncomingConnections : 2000

auditLog:
 destination : file
 format : JSON
 path : /data/GDRadius/Log/GDRadiusAduit.json
 filter : '{ atype : {\$nin : ["authenticate"]} }'

security:
 authorization  : enabled
EOF_GDRadius

mongod -f /data/GDRadius/Config/GDRadius.conf


############## 初始化mongodb2 port:40004 ##############
# GDRadius实例创建目录
mkdir -p /data/iGD/Data
mkdir -p /data/iGD/Log
mkdir -p /data/iGD/Config
mkdir -p /data/iGD/Keys

# 生成配置文件
cat > /data/iGD/Config/iGD.conf <<EOF_GDRadius
processManagement :
 fork : true
 pidFilePath :  /data/iGD/iGD.pid
systemLog:
 destination : file
 path : /data/iGD/Log/iGD.log
 logAppend : true

storage:
 dbPath : /data/iGD/Data
 engine : wiredTiger
 directoryPerDB : true

net:
 port : 40004
 maxIncomingConnections : 2000

auditLog:
 destination : file
 format : JSON
 path : /data/iGD/Log/iGDAduit.json
 filter : '{ atype : {\$nin : ["authenticate"]} }'
EOF_GDRadius

# 初始化实例30004设置密码
mongod -f /data/iGD/Config/iGD.conf
mongo --host 127.0.0.1 --port 40004 --quiet 'admin' <<EOJS
db.createUser(
  {
        user:"admin",
        pwd:"1234-qwer",
        roles:
		[ 	{ "role" : "readWrite", "db" : "admin" }, 
			{ "role" : "dbAdmin", "db" : "admin" }, 
			{ "role" : "userAdmin", "db" : "admin" }, 
			{ "role" : "clusterAdmin", "db" : "admin" }, 
			{ "role" : "readAnyDatabase", "db" : "admin" }, 
			{ "role" : "readWriteAnyDatabase", "db" : "admin" }, 
			{ "role" : "userAdminAnyDatabase", "db" : "admin" }, 
			{ "role" : "dbAdminAnyDatabase", "db" : "admin" } ]
    })
EOJS

mongod --shutdown --dbpath /data/iGD/Data/

# 启动密码认证
cat > /data/iGD/Config/iGD.conf <<EOF_GDRadius
processManagement :
 fork : true
 pidFilePath :  /data/iGD/iGD.pid
systemLog:
 destination : file
 path : /data/iGD/Log/iGD.log
 logAppend : true

storage:
 dbPath : /data/iGD/Data
 engine : wiredTiger
 directoryPerDB : true

net:
 port : 40004
 maxIncomingConnections : 2000

auditLog:
 destination : file
 format : JSON
 path : /data/GDRadius/Log/iGDAduit.json
 filter : '{ atype : {\$nin : ["authenticate"]} }'

security:
 authorization  : enabled
EOF_GDRadius

mongod -f /data/iGD/Config/iGD.conf


############## 环境清理 ###############
/etc/init.d/mysql stop
mongod --shutdown --dbpath /data/GDRadius/Data/
mongod --shutdown --dbpath /data/iGD/Data/
# /etc/init.d/rc.radiusd.iGuangdong stop




############## monit配置文件 ##############
	
echo 'export LD_LIBRARY_PATH=/home/radius/radius_iGuangdong/lib' > /etc/profile.d/radiusd.sh
source /etc/profile.d/radiusd.sh
# /etc/init.d/rc.radiusd.iGuangdong start

# 生成mysql启动配置文件
cat > /etc/monit.d/mysqld <<EOF_MYSQL_MONIT
check process mysql with pidfile /var/run/mysqld/mysqld.pid
  start program = "/etc/init.d/mysql start" with timeout 60 seconds
  stop program = "/etc/init.d/mysql stop"
  if failed unixsocket /var/lib/mysql/mysql.sock then restart
EOF_MYSQL_MONIT

cat > /etc/monit.d/mongodb_30004 <<EOF_GDRadius_MONIT
check process mongodb_30004 pidfile /data/GDRadius/GDRadius.pid
  start program = "/usr/bin/mongod -f /data/GDRadius/Config/GDRadius.conf"
  stop program = "/usr/bin/mongod --shutdown --dbpath /data/GDRadius/Data/"
  if failed port 30004  type tcp then restart
EOF_GDRadius_MONIT

cat > /etc/monit.d/mongodb_40004 <<EOF_iGD_MONIT
check process mongodb_40004 pidfile /data/iGD/iGD.pid
  start program = "/usr/bin/mongod -f /data/iGD/Config/iGD.conf"
  stop program = "/usr/bin/mongod --shutdown --dbpath /data/iGD/Data/"
  if failed port 40004  type tcp then restart
EOF_iGD_MONIT

cat > /etc/monit.d/radius <<EOF_RADIUSD_MONIT
check process radius with pidfile /home/radius/radius_iGuangdong/var/run/radiusd/radiusd.pid
  start program = "/etc/init.d/rc.radiusd.iGuangdong start"
  stop program = "/etc/init.d/rc.radiusd.iGuangdong stop"
  if failed host 127.0.0.1 port 1812 type udp protocol radius secret testing123 then restart
  if failed host 127.0.0.1 port 1813 type udp protocol radius secret testing123 then restart
EOF_RADIUSD_MONIT

exec "$@"